class FabricAsset {
	constructor(persistence) {
		this.persistence = persistence;
	}
	getObj(txObj) {
		console.log('-----------obg-----111-----');
		
		return;
	}
   	async getAssetTxobj(txObj , network_name , channel_genesis_hash) {
		const rwset =txObj.payload.data.actions[0].payload.action.proposal_response_payload.extension.results.ns_rwset
		const txhash = txObj.payload.header.channel_header.tx_id
		const writeSet = rwset.map(i => ({
			chaincode: i.namespace,
			set: i.rwset.writes
		}));
		const write_set = JSON.stringify(writeSet, null, 2);
		var write_data = JSON.parse(write_set)
		if(write_data[1].set.length !== 0 ){
			var insert_data = write_data[1].set[0].value
			var res_data = JSON.parse(insert_data);
			if(res_data.Phone && res_data.Money) {
				var user_row = {
					name:res_data.Name,
					sex:res_data.Sex,
					age:res_data.Age,
					phone:res_data.Phone,
					userId:res_data.ID,
					money:res_data.Money,
					txid:txhash,
					network_name: network_name,
					channel_genesis_hash:channel_genesis_hash
				};
				const res = await this.persistence
					.getCrudService()
					.saveUserOrAsset(network_name, user_row , 'fabric_user');
			}
			
			if(res_data.Type == 'TOKEN' && res_data.OwnerID) {
				var asset_row = {
					name:res_data.Name,
					owner_id:res_data.OwnerID,
					matadata:res_data.Matedata,
					type:res_data.Type,
					asset_id:res_data.ID,
					txid:txhash,
					network_name: network_name,
					channel_genesis_hash:channel_genesis_hash
				};
				const res = await this.persistence
					.getCrudService()
					.saveUserOrAsset(network_name, asset_row , 'fabric_asset');
			}

			if(res_data.Sender && res_data.Receiver && res_data.Amount) {
				var send_monet_row = {
					tid:res_data.TID,
					senderId:res_data.Sender,
					receiverId:res_data.Receiver,
					amount:res_data.Amount,
					created_time:res_data.Time,
					txid:txhash,
					network_name: network_name,
					channel_genesis_hash:channel_genesis_hash
				};
				const res = await this.persistence
					.getCrudService()
					.saveUserOrAsset(network_name, send_monet_row , 'user_transaction_history');
			}
			if(write_data[1].set[1] !== undefined) {
				var insert_data_asset = write_data[1].set[1].value;
				var res_asset_data = JSON.parse(insert_data_asset);
				if (res_asset_data.AID && res_asset_data.AssetID && res_asset_data.Sender) {
					var send_asset_row = {
						assetId:res_asset_data.AssetID,
						senderId:res_asset_data.Sender,
						receiverId:res_asset_data.Receiver,
						aid:res_asset_data.AID,
						created_time:res_asset_data.Time,
						txid:txhash,
						network_name: network_name,
						channel_genesis_hash:channel_genesis_hash
					};
					const res = await this.persistence
						.getCrudService()
						.saveUserOrAsset(network_name, send_asset_row , 'asset_transfer_history');
				}
				
			}
		}

		return;
	}
}

module.exports = FabricAsset;